package com.zfp.sys.service;

import com.zfp.core.service.BaseService;
import com.zfp.core.utils.Md5Util;
import com.zfp.sys.dao.SysAccountDao;
import com.zfp.sys.dao.SysRoleDao;
import com.zfp.sys.dao.SysUserDao;
import com.zfp.sys.entity.SysAccount;
import com.zfp.sys.entity.SysRole;
import com.zfp.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserService extends BaseService<SysUser>{
	@Autowired
	private SysUserDao sysUserDao;

	@Autowired
	private SysAccountDao sysAccountDao;
	@Autowired
	private SysRoleDao sysRoleDao;
	

	//插入和字修改
	public Boolean save(SysUser sysUser){
		int row=0;
		//System.out.println(sysUser.getUserId());
		if(sysUser.getUserId() == null){
			sysUser.setDeleted("n");

			row=sysUserDao.insert(sysUser);

			SysAccount sysAccount = new SysAccount();
			sysAccount.setUserId(sysUser.getUserId());
			sysAccount.setLoginName(sysUser.getLoginName());
			sysAccount.setPassword(Md5Util.md5L32("00000000"));

			sysAccountDao.insert(sysAccount);

			saveUserRoleTr(sysUser.getUserId().toString(),"3",null);


		}else{
			
			row=sysUserDao.update(sysUser);
			//System.out.println("update: "+row);
		}
		boolean flag=row>=1?true:false;
		return flag;
	}

	//逻辑删除
	public Boolean deleteById(String id){
		SysUser sysUser = new SysUser();
		sysUser.setUserId(Long.parseLong(id));
		sysUser.setDeleted("y");
		int row=sysUserDao.update(sysUser);
		return row>0;
	}

	/**
	 * 得到该用户下的角色select的 option字符串
	 * @param userId
	 * @return 角色的select的 option字符串String
	 */
	public  Map<String,String> getRoleSelectMap(Long userId,SysUser sysUserSession) {
		//取到全部角色
		SysRole sysRole =  new SysRole();
		List<SysRole> sysRoleList = null;
		//除了超级用户（角色id为1的用户系统认为是超级用户）其他的用户只能看到自己新建或自己有的角色
//		if(sysRoleService.superRoleOrFalse(sysUserSession)=="superRoleFalse"){
//			sysRole.setSuperRole("superRoleFalse");
//			sysRole.setSuperRoleUserId(sysUserSession.getId());
//			sysRoleList = sysRoleDao.list(sysRole);
//		}else{
			sysRoleList = sysRoleDao.list(sysRole);
//		}



		//取得该用户下的角色ID
		List<SysRole> sysUserRoleAssignList = sysRoleDao.list("SysRole.findUserRoles", userId);
		//Map存放该用户下的角色ID用于判断该用户下的角色树
		Map<String,String> roleIdMap = new HashMap<String,String>();
		for(SysRole sysUserRoleAss:sysUserRoleAssignList){
			roleIdMap.put(sysUserRoleAss.getRoleId().toString(), "");
		}
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		for (SysRole role : sysRoleList) {
			if(roleIdMap.containsKey(role.getRoleId().toString())){//用户有该角色
				sb2.append("<option value=\""+role.getRoleId().toString()+"\">"+role.getRoleName()+"</option>");
			}else{
				sb1.append("<option value=\""+role.getRoleId().toString()+"\">"+role.getRoleName()+"</option>");
			}
		}
		Map<String,String> selectMap = new HashMap<String,String>();
		selectMap.put("select1", sb1.toString());
		selectMap.put("select2", sb2.toString());
		return selectMap;
	}
	/**
	 * userId用户的角色保存
	 * @param userId  用户ID
	 * @param roleIds 角色ID串
	 * @return
	 */
	public Boolean saveUserRoleTr(String userId,String roleIds,Long sessionuserId){
		sysUserDao.deleteById("SysUser.deleteUserRole", Long.parseLong(userId));
		sysUserDao.deleteById("SysUser.deleteDoctor", Long.parseLong(userId));
		String[] roleIdArr = roleIds.split(",");

		for(String roleId : roleIdArr){
			if(roleId!=null&&!"".endsWith(roleId)){
				SysUser sysUserRoleAssign =  new SysUser();
				sysUserRoleAssign.setRoleId(roleId);
				sysUserRoleAssign.setUserId(Long.parseLong(userId));
				sysUserDao.insert("SysUser.insertUserRole",sysUserRoleAssign);
				if(roleId.equals("4")){
					sysUserDao.insert("SysUser.insertDoctor",sysUserRoleAssign);
				}
			}
		}


		return true;
	}

	//登录获取用户
	public SysUser getLoginUser(String loginName,String password){
		SysUser sysUserP = new SysUser();
		//System.out.println(loginName+"-----"+password);
		sysUserP.setLoginName(loginName);
		sysUserP.setPassword(Md5Util.md5L32(password));
		SysUser sysUser = sysUserDao.getObject("SysUser.getLoginUser",sysUserP);
		return sysUser;
	}




}
